<template>
  <el-card shadow="never" class="aui-card--fill">
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="150px" labelPosition="right" inline-message>
      <el-row :gutter="20">
        <el-col :span="12" :offset="2">
          <el-form-item :label="$t('securityconfig.initPassword')" prop="initPassword">
            <el-input v-model="dataForm.initPassword"></el-input>
            <span class="form-item-explain" v-html="$t('securityconfig.initPasswordMessage')"></span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="2">
          <el-form-item :label="$t('securityconfig.initPasswordModify')" prop="initPasswordModify">
            <el-select v-model="dataForm.initPasswordModify">
              <el-option v-for="template in this.$getDictList(this.PWD_INIT, false)" :key="template.val" :label="template.label" :value="parseInt(template.val)"></el-option>
            </el-select>
            <span class="form-item-explain" v-html="$t('securityconfig.initPasswordModifyMessage')"></span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="2">
          <el-form-item :label="$t('securityconfig.passwordModifySecurityLevel')" prop="passwordModifySecurityLevel">
            <el-select v-model="dataForm.passwordModifySecurityLevel" :placeholder="$t('securityconfig.passwordModifySecurityLevel')">
              <el-option v-for="template in this.$getDictList(this.PWD_LEVEL, false)" :key="template.val" :label="template.label" :value="parseInt(template.val)">
              </el-option>
            </el-select>
            <span class="form-item-explain" v-html="$t('securityconfig.passwordModifySecurityLevelMessage')"></span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="2">
          <el-form-item :label="$t('securityconfig.passwordModifyCycle')" prop="passwordModifyCycle">
            <el-input v-model.number="dataForm.passwordModifyCycle">
              <template slot="append">天</template>
            </el-input>
            <span class="form-item-explain" v-html="$t('securityconfig.passwordModifyCycleMessage')"></span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="2">
          <el-form-item :label="$t('securityconfig.passwordModify')" prop="passwordModify">
            <el-select v-model="dataForm.passwordModify">
              <el-option v-for="template in this.$getDictList(this.PWD_MODIFY, false)" :key="template.val" :label="template.label" :value="parseInt(template.val)"></el-option>
            </el-select>
            <span class="form-item-explain" v-html="$t('securityconfig.passwordModifyMessage')"></span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="2">
          <el-form-item :label="$t('securityconfig.failureHere')" prop="failureHere">
            <el-input v-model.number="dataForm.failureHere">
              <template slot="append">次</template>
            </el-input>
            <span class="form-item-explain" v-html="$t('securityconfig.failureHereMessage')"></span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="2">
          <el-form-item :label="$t('securityconfig.unlockTime')" prop="unlockTime">
            <el-input v-model.number="dataForm.unlockTime">
              <template slot="append">分钟</template>
            </el-input>
            <span class="form-item-explain" v-html="$t('securityconfig.unlockTimeMessage')"></span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="2">
          <el-form-item :label="$t('securityconfig.verificationCodeHere')" prop="verificationCodeHere">
            <el-input v-model.number="dataForm.verificationCodeHere">
              <template slot="append">次</template>
            </el-input>
            <span class="form-item-explain" v-html="$t('securityconfig.verificationCodeHereMessage')"></span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="6">
          <el-button v-if="$hasPermission('sys:securityconfig:update')" type="primary" :loading="loading" @click="dataFormSubmitHandle">{{ $t('confirm') }}</el-button>
        </el-col>
      </el-row>
    </el-form>
  </el-card>
</template>
<style scoped>
.demonstration {
  font-size: 12px;
  color: #8492a6;
  line-height: 20px;
  position: relative;
  float: left;
  padding-top: 5px;
  padding-bottom: 10px;
}

.el-form-item {
  margin-bottom: 2px;
}

.el-select {
  display: inline;
  position: relative;
}

/*.el-form-item__content {*/
/*line-height: 10px;*/
/*position: relative;*/
/*}*/
</style>
<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      PWD_LEVEL: 'PWD_LEVEL',
      PWD_INIT: 'PWD_INIT',
      PWD_MODIFY: 'PWD_MODIFY',
      loading: false,
      dataForm: {
        initPassword: null,
        initPasswordModify: 0,
        passwordModifySecurityLevel: 1,
        passwordModify: 0,
        passwordModifyCycle: 0,
        failureHere: 0,
        unlockTime: 1,
        verificationCodeHere: 1
      }
    }
  },
  activated () {
    this.init()
  },
  computed: {
    dataRule () {
      return {
        initPassword: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        initPasswordModify: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        passwordModifySecurityLevel: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        passwordModify: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        passwordModifyCycle: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        failureHere: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        unlockTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        verificationCodeHere: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.loading = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.getInfo()
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get('/sys/security/config').then(({ data: res }) => {
        if (res.code !== 0) {
          return
        }
        this.dataForm = res.data
      }).catch(() => { })
      this.loading = false
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        this.$http.post('/sys/security/saveConfig', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            this.loading = false
            return
          }
          this.loading = false
          this.$message({
            message: '配置保存成功',
            type: 'success',
            duration: 3000,
            onClose: () => {
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
